﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表单模板管理器
    /// </summary>
    public class FormTemplateManager : ManagerBase
    {
        /// <summary>
        /// 获取或设置默认的表单模板管理器
        /// </summary>
        public static FormTemplateManager Default { get; set; }
        /// <summary>
        /// 将指定文件加入模板安装队列
        /// </summary>
        /// <param name="installerId">
        /// 模板创建者ID
        /// </param>
        /// <param name="fileId">
        /// 要安装的模板文件ID
        /// </param>
        /// <param name="tempName">
        /// 模板文件名称
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult InstallTemplate(Guid installerId, Guid fileId, string tempName)
        {
            //检查名称
            if (tempName == string.Empty)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050101,
                    Message = "目标模板名称不能为空！"
                };
            }
            //检查文件描述是否存在
            FileDescription des = this.Components.FileDescriptionProvider.Get(fileId);
            if (des == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050102,
                    Message = "指定文件描述不存在！"
                };
            }
            //检查目录
            if (des.Path != "FormTemplates")
            {
                return new OperateResult()
                {
                    ResultCode = 0x050103,
                    Message = "文件访问违规！"
                };
            }
            //检查文件是否存在
            if (!this.Components.PhysicalFileManager.FileExists(des))
            {
                return new OperateResult()
                {
                    ResultCode = 0x050104,
                    Message = "指定文件已从磁盘移除！"
                };
            }
            //检查文件是否已被安装
            if (this.Components.FormTemplateProvider.CheckFileInstalled(fileId) == true)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050105,
                    Message = "同一文件不能重复安装！"
                };
            }
            //更新文件备注
            if (!this.Components.FileDescriptionProvider.UpdateTag(des.Identity, "Default:等待安装"))
            {
                return new OperateResult()
                {
                    ResultCode = 0x050106,
                    Message = "更新文件备注失败！"
                };
            }
            //创建并添加模板
            FormTemplate temp = this.Components.FormTemplateProvider.Create();
            temp.Installer = installerId;
            temp.Name = tempName;
            temp.PhysicalFileId = fileId;
            temp.State = FormTemplateState.Created;
            bool res = this.Components.FormTemplateProvider.Add(temp);

            if (!res)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050107,
                    Message = "创建模板失败！"
                };
            }
            //开始表单安装服务
            BuildService.FormTemplateCompiler.Default.Start();

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 查询名称中包含指定字符的表单模板集合
        /// </summary>
        /// <param name="nameLike">
        /// 要查询的表单模板名称
        /// </param>
        /// <param name="limit">
        /// 限制数量
        /// </param>
        /// <param name="skip">
        /// 返回数据的起始位置
        /// </param>
        /// <param name="isAll">
        /// 一个值，表示返回的结果是所有的表单模板还是已安装的表单模板
        /// </param>
        /// <returns>
        /// 查询结果
        /// </returns>
        public OperateResult QueryByName(string nameLike, int skip = 0, int limit = 5, bool isAll = false)
        {
            if (limit > 50 || limit <= 0)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050201,
                    Message = "limit必须在1-50之间！"
                };
            }

            return OperateResult.SuccessResult(this.Components.FormTemplateProvider.QueryByName(nameLike, skip, limit, isAll));
        }
        /// <summary>
        /// 通过指定ID取得表单模板实例
        /// </summary>
        /// <param name="id">
        /// 要去的的表单模板的ID
        /// </param>
        /// <param name="fullModel">
        /// 是否返回完整的模型
        /// </param>
        /// <returns>
        /// 取得的表单模板
        /// </returns>
        public OperateResult GetByID(Guid id, bool fullModel = false)
        {
            FormTemplate temp = this.Components.FormTemplateProvider.Get(id);
            if (temp == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050301,
                    Message = "指定ID的表单模板不存在！"
                };
            }
            if (fullModel)
            {
                return OperateResult.SuccessResult(temp);
            }
            else
            {
                return OperateResult.SuccessResult(new
                {
                    name = temp.Name,
                    id = temp.Identity
                });
            }

        }
        /// <summary>
        /// 取得指定的文件安装后的表单模板
        /// </summary>
        /// <param name="fileId">
        /// 要取得的表单模板的文件ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult GetByFileId(Guid fileId)
        {
            FormTemplate temp = this.Components.FormTemplateProvider.GetByFileId(fileId);
            if (temp == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x050401,
                    Message = "指定模板定义文件没有安装或者安装后的模板已被删除！"
                };
            }

            return OperateResult.SuccessResult(temp);
        }
        /// <summary>
        /// 初始化静态变量
        /// </summary>
        static FormTemplateManager()
        {
            Default = new FormTemplateManager();
        }
    }
}
